package com.caxxb.controller;

import com.caxxb.common.R;
import com.caxxb.entity.GameType;
import com.caxxb.service.GameTypeService;
import com.caxxb.service.impl.GameTypeServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/gametype")
public class GameTypeController {

    private GameTypeService gameTypeService=new GameTypeServiceImpl();

    //获取所有数据类型
    @GetMapping("gettypes")
    public R getTypes(){
        List<GameType> types = gameTypeService.getTypes();
        return R.success(types);
    }

    //往session中存放typeid的方法
    @GetMapping("type/{typeid}")
    public R gamepartition(@PathVariable int typeid, HttpServletRequest request) throws ServletException, IOException {
        request.getSession().setAttribute("typeid",typeid);
        return R.success(typeid);
    }


    //获取类型名
    @GetMapping("gettypename")
    public R getTypeName(HttpSession session){
        int typeid=1;
        if(session.getAttribute("typeid")!=null){
          typeid  = (int) session.getAttribute("typeid");
        }
        String typeName = gameTypeService.getTypeName(typeid);
        return R.success(typeName);
    }
}
